package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McOrgInfoMapper;
import cc.rengu.igas.momp.common.entity.TMMcOrgInfo;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/25 16:13
 * @description 营销活动机构规则数据库操作接口实现
 */
public class McOrgInfoMapperImpl implements McOrgInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 营销活动机构信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return cc.rengu.igas.momp.common.entity.TMMcOrgInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/31 20:03
     */
    @Override
    public List<TMMcOrgInfo> selectMcOrgInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcOrgInfoList");


        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcOrgInfo, TMMcOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动机构信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcOrgInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public TMMcOrgInfo selectMcOrgInfo(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcOrgInfoList");


        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mcOrgInfo, mcOrgInfo);
        if (0 != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>营销活动编号<{}>获取代理商营销活动信息失败!<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return mcOrgInfo;
    }

    /**
     * 营销活动机构信息列表新增接口实现
     * 带事务控制的流程使用
     *
     * @param mcOrgInfoList 营销活动机构信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 21:53
     */
    @Override
    public int insertMcOrgInfoListByBank(List<TMMcOrgInfo> mcOrgInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcOrgInfoListByBank");

        for (TMMcOrgInfo tmMcOrgInfo : mcOrgInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcOrgInfo, TMMcOrgInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>机构号<{}>的营销活动机构信息失败!<{}>", tmMcOrgInfo.getInstId(), tmMcOrgInfo.getMcNo(), tmMcOrgInfo.getOrgId(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动机构信息列表更新接口实现
     *
     * @param mcOrgInfoList
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 21:53
     */
    @Override
    public int updateMcOrgInfoListByBank(List<TMMcOrgInfo> mcOrgInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcOrgInfoListByBank");

        for (TMMcOrgInfo tmMcOrgInfo : mcOrgInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcOrgInfo, TMMcOrgInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新法人机构号<{}>营销活动编号<{}>机构号<{}>的营销活动机构信息失败!<{}>", tmMcOrgInfo.getInstId(), tmMcOrgInfo.getMcNo(), tmMcOrgInfo.getOrgId(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 营销活动机构信息列表删除接口实现
     * 删除指定营销活动的所有机构信息
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 21:53
     */
    @Override
    public int deleteMcOrgInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteMcOrgInfoListByBank");

        TMMcOrgInfo tmMcOrgInfo = new TMMcOrgInfo();
        tmMcOrgInfo.setInstId(instId);
        tmMcOrgInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcOrgInfo, TMMcOrgInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动机构信息失败!<{}>", tmMcOrgInfo.getInstId(), tmMcOrgInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条营销活动机构信息数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcOrgInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 23:55
     */
    @Override
    public int insertMcOrgInfo(String instId, TMMcOrgInfo mcOrgInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcOrgInfoListByBank");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mcOrgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记营销活动机构信息失败,instId:<{}>,mcNo:<{}>,iReturnCode:<{}>", mcOrgInfo.getInstId(), mcOrgInfo.getMcNo(), iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 修改单条营销活动机构信息数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcOrgInfo
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 23:55
     */
    @Override
    public int updateMcOrgInfo(String instId, TMMcOrgInfo mcOrgInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcOrgInfoListByBank");
        mcOrgInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mcOrgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新营销活动机构信息失败,instId:<{}>,mcNo:<{}>,iReturnCode:<{}>",
                    mcOrgInfo.getInstId(), mcOrgInfo.getMcNo(), iReturnCode);
        }
        return iReturnCode;
    }

    /**
     * 使用主键删除单条营销活动机构信息数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param orgId
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 23:55
     */
    @Override
    public int deleteMcOrgInfoByPrimaryKey(String instId, String mcNo, String orgId) throws Exception {
        return 0;
    }

    /**
     * 使用主键查询单条营销活动机构信息数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param orgId
     * @return int
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 23:55
     */
    @Override
    public int selectMcOrgInfoByPrimaryKey(String instId, String mcNo, String orgId) throws Exception {
        return 0;
    }


    /**
     * 营销活动机构信息查询接口定义
     *
     * @param instId     法人机构号
     * @param signInstId 签约机构
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcOrgInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/28
     */
    @Override
    public List<TMMcOrgInfo> selectMcOrgInfoBySignInstId(String instId, String signInstId) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], SIGN_INST_ID=[" + signInstId + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMcOrgInfoListByOrgId");

        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setOrgId(signInstId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcOrgInfo, TMMcOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动机构信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcOrgInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 营销活动机构信息列表查询接口实现
     *
     * @param instId 法人机构号
     * @param OrgId  机构号
     * @return cc.rengu.igas.momp.common.entity.TMMcOrgInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/31 20:03
     */
    @Override
    public List<TMMcOrgInfo> selectMcOrgInfoListByinstIdOrgId(String instId, String OrgId) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], OrgId=[" + OrgId + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcOrgInfoListByinstIdOrgId");


        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setOrgId(OrgId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcOrgInfo, TMMcOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动机构信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcOrgInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 查询机构及其下属的所有机构
     *
     * @param instId 法人机构号
     * @param OrgId  机构号
     * @return cc.rengu.igas.momp.common.entity.TMMcOrgInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/31 20:03
     */
    @Override
    public List<TMMcOrgInfo> selectAllMcOrgInfoListByinstIdOrgId(String instId, String OrgId) throws Exception {
        String queryClause = "INST_ID=[" + instId + "], OrgId=[" + OrgId + "]";
        dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAllMcOrgInfoListByinstIdOrgId");


        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setOrgId(OrgId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcOrgInfo, TMMcOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取营销活动机构信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcOrgInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 查询机构及其上属的所有机构
     *
     * @param instId 法人机构号
     * @param OrgId  机构号
     * @return cc.rengu.igas.momp.common.entity.TMMcOrgInfo
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/9/10 20:03
     */
    @Override
    public List<TMMcOrgInfo> selectAllFrontMcOrgInfoListByInstIdOrgId(String instId, String OrgId) throws Exception {
        String queryClause = "INST_ID=[" + instId + "], OrgId=[" + OrgId + "]";
        dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAllFrontMcOrgInfoListByInstIdOrgId");

        // 按接口赋查询条件
        TMMcOrgInfo mcOrgInfo = new TMMcOrgInfo();
        mcOrgInfo.setInstId(instId);
        mcOrgInfo.setOrgId(OrgId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcOrgInfo, TMMcOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取机构及上属机构信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcOrgInfo) (item)).collect(Collectors.toList());
    }

}
